<?php
class Contact extends MY_Front_Controller {

	function __construct() {
		parent::__construct();
	}
	
    function index()
    {
        $this->load->helper('form');
        $this->load->library ( 'form_validation' );
        $this->form_validation->set_rules ( 'name', lang('name'), 'required' );
        $this->form_validation->set_rules ( 'email', lang('email'), 'required|email' );
        
        $data = array();
        
        $contact_time = $this->config->item('contact_time');
        $data['contact_time'] = explode(';',$contact_time);

        $this->template->add_js('application/assets/js/contact.js');
        //$this->template->add_js('application/assets/js/jqueryPlugins/jquery.validate.js');
      
        if ($this->form_validation->run () == FALSE) {
        	$this->template->write('pageTitle', lang('contact',false));
	        $this->template->write_view ( 'content', 'front/contact/contact.php', $data );
	        $this->template->render ();
        }

    }
    public function send_form()
    {
        $this->load->library('email');
        $response = array('status'=>1,'message'=>'');
        $data = array();
        $nume = ($this->input->post('nume'))?$this->input->post('nume'):'';
        $email = ($this->input->post('email'))?$this->input->post('email'):'';
        $mesaj = ($this->input->post('mesaj'))?$this->input->post('mesaj'):'';
        $phone = ($this->input->post('phone'))?$this->input->post('phone'):'';
        $contact_by = ($this->input->post('contact_by'))?$this->input->post('contact_by'):'';
        $contact_time = ($this->input->post('contact_time'))?$this->input->post('contact_time'):'';

        $data['nume']  = $nume;
        $data['email'] = $email;
        $data['mesaj'] = $mesaj;
        $data['contact_time'] = $contact_time;
        $data['contact_by'] = $contact_by;
        $data['phone'] = $phone;

        $this->load->model('user_model');
        
        $adresses = $this->user_model->getEmailAdresses($this->config->item('lang_obj')->id);
              
        $send_to = '';
        if (!empty($adresses)){
        	foreach ($adresses as $addr){
        		$send_to .= $addr->user_email . ',';
        	}
        }
        
        if (strlen($send_to)){
        	$send_to = substr($send_to, 0, -1);
        }
        
        
        $message = $this->load->view('front/contact/contact_form.php',$data,true);
        $this->email->from(htmlentities(strip_tags($email)), '' );
        $this->email->to($send_to);
        $this->email->mailtype = 'html';
        $this->email->subject(lang('contact_form'));
        $this->email->message($message);
        if ($this->email->send()){
        		$response['status'] = 1;
        }
        else{
                $response['status'] = 0;
        }
        echo json_encode($response);
    }
}